package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.CheckGroupCheckItemDao;
import com.itheima.mapper.CheckGroupDao;
import com.itheima.mapper.CheckItemDao;
import com.itheima.mapper.SetmealCheckGroupDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.ICheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ICheckGroupServiceImpl implements ICheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Autowired
    private CheckGroupCheckItemDao checkGroupCheckItemDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;

    /**
     * 添加检查项到项目组
     *
     * @param checkItemIds
     * @param checkGroup
     * @return
     */
    @Override
    @Transactional
    public Result add(String checkItemIds, CheckGroup checkGroup) {

        if (checkItemIds == null || checkItemIds.equals("")) {
            return Result.of(false, "新增检查组失败!", null);
        }
        List<String> ids = Arrays.asList(checkItemIds.split(","));

        List<CheckItem> checkItemList = checkItemDao.findByIds(ids);

        //检查项是否存在
        if (ids.size() != checkItemList.size()) {
            return Result.of(false, "新增检查组失败!", null);
        }

        checkGroupDao.add(checkGroup);

        checkGroupCheckItemDao.addRelation(checkGroup.getId(), ids);

        return Result.of(true, "新增检查组成功", null);
    }

    /**
     * 根据id删除检查组
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteById(String id) {

        int count = setmealCheckGroupDao.containCheckGroup(id);
        if(count > 0){
            return Result.of(false, "套餐中包含该检查组无法删除!", null);
        }
        int affect = checkGroupCheckItemDao.deleteByCheckGroupId(id);
        if (affect == 0) {
            return Result.of(false, "删除检查组失败!", null);
        }
        checkGroupDao.delete(id);

        return Result.of(true, "删除检查组成功", null);
    }

    /**
     * 根据套餐id查询关联的检查项id
     *
     * @param id
     * @return
     */
    @Override
    public Result findCheckGroupIdsBySetmealId(String id) {
        List<Integer> checkGroupIds = setmealCheckGroupDao.findCheckGroupIdsBySetmealId(id);

        return Result.of(true, "查询检查组成功", checkGroupIds);
    }

    /**
     * 分页查询检查组
     *
     * @param query
     * @return
     */
    @Override
    public PageResult<CheckGroup> findPage(QueryPageBean query) {

        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());

        List<CheckGroup> checkGroupList = checkGroupDao.selectByCondition(query.getQueryString());

        PageInfo<CheckGroup> pageInfo = new PageInfo<>(checkGroupList);

        return new PageResult<>(pageInfo.getTotal(), checkGroupList);
    }

    /**
     * 编辑检查组
     *
     * @param checkItemIds
     * @param checkGroup
     * @return
     */
    @Override
    @Transactional
    public Result edit(String checkItemIds, CheckGroup checkGroup) {

        if (checkGroup.getId() == null){
            return Result.of(false, "参数有误!", null);
        }

        if (checkItemIds == null || checkItemIds.equals("")) {
            return Result.of(false, "检查项为空!", null);
        }
        List<String> ids = Arrays.asList(checkItemIds.split(","));

        List<CheckItem> checkItemList = checkItemDao.findByIds(ids);

        //检查项是否存在
        if (ids.size() != checkItemList.size()) {
            return Result.of(false, "检查项不存在!", null);
        }

        //修改检查组
        int affect = checkGroupDao.edit(checkGroup);

        if(affect == 0){
            return Result.of(false, "编辑检查组失败", null);
        }

        //删除原有检查组和检查项关系
        checkGroupCheckItemDao.deleteByCheckGroupId(checkGroup.getId().toString());

        //添加新检查组和检查项关系
        checkGroupCheckItemDao.addRelation(checkGroup.getId(),ids);

        return Result.of(true, "编辑检查组成功", null);
    }

    /**
     * 查询全部检查组
     *
     * @return
     */
    @Override
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupDao.findAll();
        return Result.of(true, "查询检查组成功", checkGroupList);
    }

    /**
     * 根据id查询检查组
     *
     * @param id
     * @return
     */
    @Override
    public Result findById(String id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return  Result.of(true, "查询检查组成功", checkGroup);
    }
}
